<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>应急信息发布项目维护</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <link href="../../css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <script src="../../js/jquery-ui-1.10.4.custom.min.js"></script>
    <link href="../../css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css">
    
    <script>
        layui.use(['table', 'form', 'jquery', 'element','layer'], function () {
            var table = layui.table;
            var form = layui.form;
            var $ = layui.$;
            var layer=layui.layer;
            var user;
            var options = {
                elem: "#table",
                id: "modelReload",
                url: '/mgrTemplate/find',
                method: 'post',
                request: {
                    pageName: 'page',
                    limitName: 'limit'
                },
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    msgName: 'msg',
                    countName: 'count',
                    dataName: 'data'
                },
                loading: true,
                page: {
                    count: "count",
                    limit: 10,
                    limits: [10, 20, 50],
                    layout: ['prev', 'page', 'next', 'skip', 'limit', 'count'],
                    prev: "上一页",
                    next: "下一页",
                    groups: 5
                },
                cols: [[ //表头
                	{field: 'province', title: '省份',align:'center',width: '8%'},
                    {field: 'cityName', title: '地市',align:'center',width: '8%'},
                    {field: 'countyName', title: '区县',align:'center',width: '8%'},
                    {field: 'name', title: '项目名称',align:'center',width: '18%',templet:function(d){
                    	return '<div style="text-align:center">'+d.name+'</div>'
                    }},
                    {field: 'cityExtId', title: '短信通道号',align:'center',width: '12%'},
                    {field: 'mgrStateName', title: '项目状态',align:'center',width: '10%'},
                    {field: 'templateStateName', title: '审核状态',align:'center',width: '10%'},
                    {field: 'operation', title: '操作',align:'center', toolbar: '#barDemo'}
                ]],
                done: function (res) {
                    // console.log(JSON.stringify(res));
                }
            };
    
    	$("#mgrName").autocomplete({
       	source : function( request, response ) {
        var key = $("#mgrName").val();
        var cityId=$("#cityId").val();
        var countyId=$("#countyId").val();
        var url = '/util/selectMgrNameLike';
        var param = {
            cityId : cityId,
            countyId : countyId,
         key : key
        };
                 $.ajax({
         type : 'post',
         async : false,
         url : url,
         data : param,
         success : function(json) {
          var data = json.data;
          source = [];
          for (var i = 0; i < data.length; i++) {
           source[i] = data[i].name;
          }
          console.log(source);
          response( source );
         },
         error : function() {
          layer.msg('系统异常，请联系管理员处理!');
         }
        }
        );
            },
       messages : { //message设置不出现匹配关键字的结果
        noResults : '',
        results : function() {
        }
       }
      });
    		
    		table.render(options);
    		table.on('tool(tableElement)', function (obj) {
                 var data = obj.data;
                if (obj.event == 'detail') {
                    window.location.href = 'smsMgrTemplateView.html?mgrId=' + data.id;
                } else if (obj.event == 'modify') {
                	var mgrState=data.mgrState;
                	var checkState;
                	if(data.mgrState=='3'){
                		mgrState = "2";
                	}
                	if(data.templateState=='0'||data.templateState=='1'){
                		checkState = "2";
                	}else if(data.templateState=='2'){
                		checkState = "0";
                	}else if(data.templateState=='3'){
                		checkState = "1";
                	}
                    window.location.href = 'smsMgrTemplateModify.html?mgrId=' + data.id + "&mgrState="+mgrState+"&checkState="+checkState;
                } else  {
                    if (obj.event == data.mgrState) {
                        layer.msg("该项目已经是该状态了");
                    } else {
                        layer.confirm('您确定要修改该项目状态吗？', {icon: 3, title: '提示'}, function (index) {
                            var url = "/mgrTemplate/modifyMgrState";
                            var param = {
                                id: data.id,
                                cityId: data.cityId,
                                mgrState: obj.event
                            };
                            $.ajax({
                                type: 'post',
                                data: param,
                                url: url,
                                success: function (json) {
                                    if (json.code == 0) {
                                        layer.confirm('修改成功', {
                                            btn: ['确认'] //按钮
                                        }, function () {
                                            location.href = "smsMgrTemplateManage.html";
                                            //变更同步
										var turl = "/SmsChange/findChangeByMgrId";
										var tparam = {
											mgrId : data.id
										};
										$.ajax({
											type : 'post',
											data : tparam,
											url : turl,
											success : function(json) {
											console.info(json);}
										})
                                        });
                                    }else {
                                       layer.msg(json.data.msg);
                                    }
                                }
                            });
                        });
                    }
                } 
            });
            //监听项目  区县
            form.on('select(cityId)', function (data) {
            	var data2 = selectCountyId(data.value);
                appendCounty(data2);
                var data = selectMgrInfo(data.value);
                appendMgr(data);
            });
            //监听区县
            /* form.on('select(cityId)', function (data) {
                var data2 = selectCountyId(data.value);
                appendCounty(data2);
            }); */
            //根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="">请先选择地市</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }
            
            //监听项目
            form.on('select(countyId)', function (data) {
                var data1 = selectMgrInfo(data.value);
                appendMgr(data1);
            });
            //根据城市ID和区县查询项目
            function selectMgrInfo(countyId) {
                var data;
                var url = '/util/findByMgrId?countyId=' + countyId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接项目
            function appendMgr(data) {
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                    }
                    $('#mgrId').html(options);
                } else {
                    $('#mgrId').html('<option value="">该区县下没有项目</option>');
                }
                form.render();
            }
            
            //更新数据表
            $('#query').on('click', function () {
            	var mgrName = $("#mgrName").val();
                var mgrId = $("#mgrId").val();
                var cityId = $("#cityId").val();
                var countyId = $("#countyId").val();
                var mgrState = $("#mgrState").val();
                var templateState = $("#templateState").val();
                if (
                    mgrId == "" && user.duty == 2
                ) {
                    layer.msg("您必须选择一个项目");
                } else {
                    var index = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    //table.render(options);
                    table.reload('modelReload', {
                    method: 'post',
                    page: {
                        curr: 1
                    },
                    where: {
                        cityId: cityId,
                        countyId: countyId,
                        mgrId: mgrId,
                        mgrState: mgrState,
                        templateState: templateState,
                        mgrName: mgrName,
                        duty: user.duty
                    }
                });
                layer.close(index);
                }
            });
            
            $('#insert').on('click', function () {
                window.location.href = 'smsMgrTemplateAdd.html';
            });
            $("#test").click(function(){
	            	//var cityCountyList = "{\"LastUpdateTime\":\"2019-08-20 12:00:01\",\"cityCountyList\":[{\"cityId\":\"1112\",\"cityName\":\"ab\",\"countyId\":\"2222221\",\"countyName\":\"ba\"}]}";
	            	var taskDetailSynReq = "{\"taskId\":\"100001 \",\"taskName\":\"台风预警任务\",\"startTime\":\"2019-08-26 18:00:00\",\"endTime\":\"2019-08-28 08:00:00\",\"sendObject\":3,\"limitInfo\":{\"totalLimit\":\"10000\",\"dayLimit\":\"5000\"},\"mgrContent\":\"台风橙色预警，……….\",\"sendScopes\":\"320585000000,320104000000\"}";
	            	$.ajax({
	            		type: 'post',
	                    async: false,
	                    data:"taskDetailSynReq="+taskDetailSynReq,
	                    url: '/smsInterfaces/taskDetailSyn',
	                    /* data:"cityCountyList="+cityCountyList,
	                    url: '/smsInterfaces/cityCountySyn', */
	                    success: function (json) {
	                    	alert("success");
	                    }
	            	});
	            });
            
            //身份识别
            $(document).ready(function () {
                $.ajax({
                    type: 'post',
                    async: false,
                    url: '/util/findUser',
                    success: function (json) {
                        user=json;
                        if (json.duty == 1) {//地市
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            var data1 = selectMgrInfo(json.cityId);
                            appendMgr(data1);
                            form.render();
                        } else if (json.duty == 2) {//项目
                            //var data = selectMgrInfo(json.countyId);
                            appendMgr(json.list);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            form.render();
                        }else if (json.duty == 3) {//区县
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data = selectMgrInfo(json.countyId);
                            appendMgr(data);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            form.render();
                        }else if(json.duty == 4){
                            if(json.cityId==25){//省权限
                            
                            }else if(json.cityId!=25&&checkStr(json.countyId)==""){//地市权限
	                            $("#cityId").val(json.cityId);
	                            $("#cityId").attr("disabled", true);
	                            var data2 = selectCountyId(json.cityId);
	                            appendCounty(data2);
	                            var data1 = selectMgrInfo(json.cityId);
	                            appendMgr(data1);
	                            form.render();
                            }else if(json.cityId!=25&&checkStr(json.countyId)!=""){//区县权限
	                            $("#cityId").val(json.cityId);
	                            $("#cityId").attr("disabled", true);
	                            var data = selectMgrInfo(json.countyId);
	                            appendMgr(data);
	                            var data2 = selectCountyId(json.cityId);
	                            appendCounty(data2);
	                            $("#countyId").val(json.countyId);
	                            $("#countyId").attr("disabled", true);
	                            form.render();
                            }
                        } 
                    }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
            });
            //校验字符串
		    function checkStr(str,defaultVal){
			if (typeof(str) == "undefined" || str == null || str == "null") {
		        if (defaultVal == undefined) {
		            str = "";
		        } else {
		            str = defaultVal;
		        }
		    }
			return str;
		}
        });

    </script>
    <script type="text/html" id="barDemo">
        <a href="javascript:void(0)" lay-event="modify" id="modify">修改</a>
        <a href="javascript:void(0)" lay-event="detail" id="detail">查看</a>
        <a href="javascript:void(0)" lay-event="0" id="startMgr">开启项目</a>
        <a href="javascript:void(0)" lay-event="2" id="closeMgr">关闭项目</a>
    </script>
</head>
<body>
<div class="layui-content-body">
    <div class="scroll-box">
        <div class="breadcrumb-box">
            <span class="layui-breadcrumb" lay-separator="|">
                     <a href="javascript:returnIndex2()">首页&nbsp;</a>
                  <a href="#">项目维护管理</a>
                  <a href="#">短信项目维护</a>
            </span>
        </div>
        <div class="content-box">
            <div class="form-select-box">
                <div class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">省份</label>
                            <div class="layui-input-block" style="width:150px">
                                <input type="text" name="province" lay-verify="required" value="江苏"
                                       autocomplete="off" class="layui-input" disabled>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">地市</label>
                            <div class="layui-input-block" style="width:150px">
                                <select name="cityId" lay-filter="cityId" id="cityId">
                                    <option value="">全部</option>
                                    <option value="11">苏州</option>
                                    <option value="12">淮安</option>
                                    <option value="13">宿迁</option>
                                    <option value="14">南京</option>
                                    <option value="15">连云港</option>
                                    <option value="16">徐州</option>
                                    <option value="17">常州</option>
                                    <option value="18">镇江</option>
                                    <option value="19">无锡</option>
                                    <option value="20">南通</option>
                                    <option value="21">泰州</option>
                                    <option value="22">盐城</option>
                                    <option value="23">扬州</option>
                                    <option value="25">省级</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">区县</label>
                            <div class="layui-input-block" style="width:150px">
                                <select name="countyId" lay-filter="countyId" id="countyId">
                                    <option value="">请先选择地市</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">项目名称</label>
                            <div class="layui-input-block" style="width:150px">
                                <select name="mgrId" lay-filter="mgrId" id="mgrId">
                                    <option value="">请先选择区县</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">项目简称</label>
                            <div class="layui-input-block" style="width:150px">
                                <input type="text"  name="mgrName" id="mgrName" lay-verify="required" placeholder="请输入项目名称"
                                            autocomplete="off" class="layui-input" />
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">项目状态</label>
                            <div class="layui-input-block" style="width:150px">
                                <select name="mgrState" lay-filter="mgrState" id="mgrState">
                                    <option value="">请选择</option>
                                    <option value="0">开启</option>
                                    <option value="1">暂停</option>
                                    <option value="2">关闭</option>
                                    <option value="3">配置中</option>
                                </select>
                            </div>
                        </div>
                    
                        <div class="layui-inline">
                            <label class="layui-form-label">审核状态</label>
                            <div class="layui-input-block" style="width:150px">
                                <select id="templateState" name="templateState" lay-filter="templateState">
                                    <option value="">请选择</option>
                                    <option value="0">待审核</option>
                                    <option value="1">审核中</option>
                                    <option value="2">审核通过</option>
                                    <option value="3">审核不通过</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline" pane="">
                            <button class="layui-btn layui-btn-normal" id="query">查&nbsp;询</button>
                            <button class="layui-btn layui-btn-normal" id="insert" >
                                <i class="layui-icon">&#xe608;</i> 添加
                            </button>
                            <!-- <button class="layui-btn layui-btn-normal" id="test" >
                                <i class="layui-icon">&#xe608;</i> 测试
                            </button> -->
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-table">
                <table id="table" lay-filter="tableElement"></table>
            </div>
        </div>
    </div>
</div>
</body>
</html>